<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\UsersWechat;
use App\Models\UserProfile;
use App\Models\Box;
use App\Models\BoxCare;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }

    public function index(Request $request){
        $rows   =  UsersWechat::paginate(15);
        $data = [
            'rows'=>$rows,
        ];
        return view('admin/user/user_list', $data);
    }

    public function vip(Request $request){
    	$sex = intval($request->input('sex'));
    	$number = safe2($request->input('number'));
    	$weixin = safe2($request->input('weixin'));
        $query   =  UsersWechat::where('type','>',0);

        if ($sex) {
        	$query->where('sex',$sex);
        }
        if ($number) {
        	$query->where('number',$number);
        }
        if ($weixin) {
        	$query->where('weixin',$weixin);
        }
        $rows   = $query->paginate(15);

        $data = [
            'rows'=>$rows,
        ];
        return view('admin/user/user_vip_list', $data);
    }

    public function update(Request $request){
    	$id = intval($request->input('id'));;

        $UsersWechat = UsersWechat::find($id);
        if (!$UsersWechat) {
        	return $this->error('用户不存在');
        }
        if ($request->exists('memo_name')) {
            $UsersWechat->memo_name = strval($request->input('memo_name'));
        }
        $UsersWechat->save();
        return $this->response('ok');
    }

    public function getUserInfo($id){
    	$user = UsersWechat::find($id);
    	if (!$user) {
    		return $this->error('用户不存在');
    	}
        if ($user->type < 1) {
            return $this->error('该用户暂不是会员');
        }
        $profile =  UserProfile::where('wechat_id',$user->id)->first();
        if ($profile) {
            $profile->color =  unserialize($profile->color);
            $profile->thing = unserialize($profile->thing);
        }else{
            $profile = new UserProfile;
            $profile->wechat_id = $user->id;
            $profile->save();
        }
        $profile->photo = $profile->photo==''? '': url($profile->photo);
        $profile->sex = $profile->sex ==1 ? '男':'女';
        $box1 = Box::where('wechat_id',$id)->where('type',0)->count('id');
        $box2 = Box::where('wechat_id',$id)->where('type',1)->count('id');
        $box3 = Box::where('to_wechat_id',$id)->count('id');
        $boxCare1 = BoxCare::where('wechat_id',$id)->count('id');
        $boxCare2 = BoxCare::where('wechat_id',$id)->where('is_frozen',1)->count('id');
        $boxCare3 = BoxCare::where('wechat_id',$id)->where('is_frozen',0)->count('id');
        $boxArr = [
            'box1'=>$box1,
            'box2'=>$box2,
            'box3'=>$box3,
            'boxCare1'=>$boxCare1,
            'boxCare2'=>$boxCare2,
            'boxCare3'=>$boxCare3,
        ];
        $data = compact('user','profile','boxArr');
    	return $this->response($data);

    }

}
